package com.yihu.wlyy.web.patient.health;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yihu.wlyy.entity.health_guide.HealthEduArticlePatient;
import com.yihu.wlyy.service.app.health.HealthEduArticleService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.BaseController;

/**
 * 患者端：健康教育控制类
 * @author George
 *
 */
@Controller
@RequestMapping(value = "/patient/health/edu")
public class HealthEduArticleController extends BaseController {

	@Autowired
	private HealthEduArticleService healthEduArticleService;

	/**
	 * 查询文章列表
	 * @param pagesize 分页大小
	 * @return 列表
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public String list(long id, int pagesize) {
		try {
			Page<HealthEduArticlePatient> list = healthEduArticleService.findByPatient(getUID(), id, pagesize);
			JSONArray jsonArray = new JSONArray();
			if (list != null) {
				for (HealthEduArticlePatient article : list) {
					JSONObject json = new JSONObject();
					json.put("id", article.getId());
					// 文章标识
					json.put("article", article.getArticle());
					// 医生姓名
					json.put("doctorName", article.getDoctorName());
					// 文章标题
					json.put("title", article.getTitle());
					// 文章查看URL
					json.put("url", article.getUrl());
					// 文章简介
					json.put("content", article.getContent());
					// 是否已读：0已读，1未读
					json.put("read", article.getRead());
					// 添加日期
					json.put("czrq", DateUtil.dateToStrLong(article.getCzrq()));
					jsonArray.put(json);
				}
			}
			return write(200, "查询成功", "list", jsonArray);
		} catch (Exception ex) {
			error(ex);
			return invalidUserException(ex, -1, "查询失败!");
		}
	}

	/**
	 * 更新文章为已读
	 * @param article 文章标识
	 * @return
	 */
	@RequestMapping(value = "read")
	@ResponseBody
	public String read(String article) {
		try {
			healthEduArticleService.updateRead(getUID(), article);
			return success("操作成功!");
		} catch (Exception ex) {
			error(ex);
			return invalidUserException(ex, -1, "操作失败!");
		}
	}
}
